package com.flow.asyncflow.core.utils.asyncFlowMonitor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flow.asyncflow.core.annotation.FlowInfo;
import com.flow.asyncflow.core.entity.asyncFlowMonitor.*;
import com.flow.asyncflow.core.entity.log.asyncflowlog.AsyncFlowLogEntity;
import com.flow.asyncflow.core.entity.log.asyncflowlog.AsyncFlowNodeDetailLogEntity;
import com.flow.asyncflow.core.entity.log.asyncflowlog.AsyncFlowNodeLogEntity;
import com.flow.asyncflow.core.enums.ExecuteEnum;
import com.flow.asyncflow.core.utils.codegeneration.AsyncFlowVelocity;
import com.flow.asyncflow.core.utils.configuration.AsyncFlowConfiguration;
import com.flow.asyncflow.core.utils.dateutils.DateUtil;
import com.flow.asyncflow.core.utils.http.AsyncFlowHttpClientUtil;
import com.flow.asyncflow.core.utils.mongodb.MongoHelper;
import com.flow.asyncflow.core.utils.reflection.AsyncFlowReflectionUtils;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

public class AsyncFlowMonitor {


    /**
     * 查询日志监控
     *
     * @param queryAsyncFlowLogRequest 请求
     * @return 返回
     */
    public AsyncFlowMonitorVo getAsyncFlowLogByPage(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {
        AsyncFlowMonitorVo asyncFlowMonitorVo = new AsyncFlowMonitorVo();
        List<AsyncFlowLogVo> asyncFlowLogVoList = new ArrayList<>();
        int totalCount = 0;
        AsyncFlowConfiguration configInfo = new AsyncFlowConfiguration();
        MongoHelper<AsyncFlowLogEntity> mongoHelper;
        String tableName = configInfo.getMongoDBTableName();
        String tableExtName = "_" + LocalDateTime.now().getYear() + "_" + LocalDateTime.now().getMonthValue();
        if (queryAsyncFlowLogRequest.getStartDate() != null) {
            tableExtName = "_" + queryAsyncFlowLogRequest.getStartDate().getYear() + "_" + queryAsyncFlowLogRequest.getStartDate().getMonthValue();
            if (queryAsyncFlowLogRequest.getStartDate().getMonthValue() < 12 && queryAsyncFlowLogRequest.getStartDate().getYear() < 2024) {
                tableExtName = "";
            }
            queryAsyncFlowLogRequest.setStartDate(DateUtil.htParse(DateUtil.htFormat_yyyyMMdd(queryAsyncFlowLogRequest.getStartDate()) + "T00:00:00"));
        }
//        if (LocalDateTime.now().getMonthValue() < 12 && queryAsyncFlowLogRequest.getStartDate().getYear() < 2024) {
//            tableExtName = "";
//        }

        mongoHelper = new MongoHelper<>(configInfo.getMongoDBConfigStr(), configInfo.getMongoDBName(), tableName + tableExtName);
        boolean eOrderService = false;
        if (configInfo.getAppUK().equals("tcgl.java.eorderservice.api")) {
            eOrderService = true;
        }
        Bson filer = null;

        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getBusinessSerialId())) {

            if (eOrderService) {
                filer = Filters.eq("BusinessSerialId", queryAsyncFlowLogRequest.getBusinessSerialId());

            } else {
                filer = Filters.eq("businessSerialId", queryAsyncFlowLogRequest.getBusinessSerialId());
            }

        }
        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getAsyncFlowManagerName())) {
            if (filer != null) {
                filer = Filters.and(filer, Filters.eq("flowManagerName", queryAsyncFlowLogRequest.getAsyncFlowManagerName()));
            } else {
                filer = Filters.eq("flowManagerName", queryAsyncFlowLogRequest.getAsyncFlowManagerName());
            }
        }
        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName())) {
            if (filer != null) {
                if (eOrderService) {
                    filer = Filters.and(filer, Filters.eq("AsyncFlowModule", queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName()));
                } else {
                    filer = Filters.and(filer, Filters.eq("asyncFlowModule", queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName()));
                }
            } else {
                if (eOrderService) {
                    filer = Filters.eq("AsyncFlowModule", queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName());
                } else {
                    filer = Filters.eq("asyncFlowModule", queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName());
                }
            }

        }

        if (queryAsyncFlowLogRequest.getStartDate() != null) {
            if (filer != null) {
                if (eOrderService) {
                    filer = Filters.and(filer, Filters.gte("AsyncFlowDateTime", queryAsyncFlowLogRequest.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
                } else {
                    filer = Filters.and(filer, Filters.gte("asyncFlowDateTime", queryAsyncFlowLogRequest.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli()));
                }
            } else {
                if (eOrderService) {
                    filer = Filters.gte("AsyncFlowDateTime", queryAsyncFlowLogRequest.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli());
                } else {
                    filer = Filters.gte("asyncFlowDateTime", queryAsyncFlowLogRequest.getStartDate().toInstant(ZoneOffset.of("+8")).toEpochMilli());
                }
            }

        } else {

        }

        if (queryAsyncFlowLogRequest.getEndDate() != null) {
            if (filer != null) {

                if (eOrderService) {
                    filer = Filters.and(filer, Filters.lte("AsyncFlowDateTime", queryAsyncFlowLogRequest.getEndDate().toInstant(ZoneOffset.of("+0")).toEpochMilli()));
                } else {
                    filer = Filters.and(filer, Filters.lte("asyncFlowDateTime", queryAsyncFlowLogRequest.getEndDate().toInstant(ZoneOffset.of("+0")).toEpochMilli()));
                }
            } else {
                if (eOrderService) {
                    filer = Filters.lte("AsyncFlowDateTime", queryAsyncFlowLogRequest.getEndDate().toInstant(ZoneOffset.of("+0")).toEpochMilli());
                } else {
                    filer = Filters.lte("asyncFlowDateTime", queryAsyncFlowLogRequest.getEndDate().toInstant(ZoneOffset.of("+0")).toEpochMilli());
                }
            }
        }

        if (queryAsyncFlowLogRequest.getEvn() != null && !queryAsyncFlowLogRequest.getEvn().equals("all")) {
            if (filer != null) {
                if (eOrderService) {
                    filer = Filters.and(filer, Filters.eq("Environments", queryAsyncFlowLogRequest.getEvn()));
                } else {
                    filer = Filters.and(filer, Filters.eq("environments", queryAsyncFlowLogRequest.getEvn()));
                }
            } else {
                if (eOrderService) {
                    filer = Filters.eq("Environments", queryAsyncFlowLogRequest.getEvn());
                } else {
                    filer = Filters.eq("environments", queryAsyncFlowLogRequest.getEvn());
                }
            }
        }
        if (filer == null) {

            //2000-01-1 0:0:0  默认时间大于等于
            if (eOrderService) {
                filer = Filters.gte("AsyncFlowDateTime", 946656000000L);
            } else {
                filer = Filters.gte("asyncFlowDateTime", 946656000000L);
            }
        }
        try (MongoCursor<Document> documentMongoCursor = mongoHelper.findByPage(filer, queryAsyncFlowLogRequest.getPageNo(), queryAsyncFlowLogRequest.getPageSize())) {

            while (documentMongoCursor.hasNext()) {
                Document asyncFlowLogDocument = documentMongoCursor.next();
                String asyncFlowLogDocumentJson = com.mongodb.util.JSON.serialize(asyncFlowLogDocument);
                AsyncFlowLogEntity appInfo = JSONObject.parseObject(asyncFlowLogDocumentJson, AsyncFlowLogEntity.class);
                AsyncFlowLogVo asyncFlowLogVo;
                asyncFlowLogVo = getAsyncFlowNodeLogVo(appInfo);
                asyncFlowLogVoList.add(asyncFlowLogVo);
            }

        }
        totalCount = mongoHelper.getCount(filer);

        for (AsyncFlowLogVo asyncFlowLogVo : asyncFlowLogVoList) {
            asyncFlowLogVo.setLogTimeDate(cn.hutool.core.date.DateUtil.parse(asyncFlowLogVo.getLogTime()));
        }
        //asyncFlowLogVoList = asyncFlowLogVoList.stream().sorted(Comparator.comparing(AsyncFlowLogVo::getLogTimeDate)).collect(Collectors.toList());
        asyncFlowMonitorVo.getAsyncFlowLogVoList().addAll(asyncFlowLogVoList);
        asyncFlowMonitorVo.setTotalCount(totalCount);
        return asyncFlowMonitorVo;
    }


    public AsyncFlowMonitorVo getAsyncFlowLogByPageEs(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {
        AsyncFlowConfiguration configInfo = new AsyncFlowConfiguration();
        AsyncFlowMonitorVo asyncFlowMonitorVo = new AsyncFlowMonitorVo();
        getAsyncFlowLogEs(queryAsyncFlowLogRequest, asyncFlowMonitorVo);
        return asyncFlowMonitorVo;
    }

    public List<AsyncFlowLogEntity> getAsyncFlowLogEs(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest, AsyncFlowMonitorVo asyncFlowMonitorVo) {
        AsyncFlowConfiguration configInfo = new AsyncFlowConfiguration();
        List<AsyncFlowLogVo> asyncFlowLogVoList = new ArrayList<>();
        List<AsyncFlowLogEntity> asyncFlowLogEntityList = new ArrayList<>();
        try {
            //1、设置搜索条件
            JSONObject queryConditions = buildQueryConditions(queryAsyncFlowLogRequest);
            JSONObject searchResponseJsonObject = AsyncFlowHttpClientUtil.post(configInfo.getEsURL() + configInfo.getEsIndexName() + "/_search", queryConditions, JSONObject.class);
            //搜索结果的总数量
            long total = searchResponseJsonObject.getJSONObject("hits").getLong("total");
            JSONArray searchHits = new JSONArray();
            searchHits = searchResponseJsonObject.getJSONObject("hits").getJSONArray("hits");
            for (Object searchHit : searchHits) {
                JSONObject searchHitJsonObject = (JSONObject) searchHit;
                String result = searchHitJsonObject.getString("_source");
                AsyncFlowLogEntity appInfo = JSONObject.parseObject(result, AsyncFlowLogEntity.class);
                asyncFlowLogEntityList.add(appInfo);
                AsyncFlowLogVo asyncFlowLogVo;
                asyncFlowLogVo = getAsyncFlowNodeLogVo(appInfo);
                asyncFlowLogVoList.add(asyncFlowLogVo);
            }

            if (asyncFlowMonitorVo != null) {
                //4、封装分页搜索结果类
                asyncFlowMonitorVo.setTotalCount((int) total);
                asyncFlowMonitorVo.setAsyncFlowLogVoList(asyncFlowLogVoList);
            }
            return asyncFlowLogEntityList;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return asyncFlowLogEntityList;
    }

    public static SearchSourceBuilder buildSearchSource(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());

        int offset = calculateOffset(queryAsyncFlowLogRequest);
        searchSourceBuilder.from(offset).size(queryAsyncFlowLogRequest.getPageSize());


        return searchSourceBuilder;
    }

    private static int calculateOffset(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {
        int offset = 0;
        if (queryAsyncFlowLogRequest.getPageNo() > 0 && queryAsyncFlowLogRequest.getPageSize() > 0) {
            offset = (queryAsyncFlowLogRequest.getPageNo() - 1) * queryAsyncFlowLogRequest.getPageSize();
        }
        return offset;
    }

    private static JSONObject buildQueryConditions(QueryAsyncFlowLogRequest queryAsyncFlowLogRequest) {
        // 创建一个空的JSON对象
        JSONObject searchJson = new JSONObject();
        JSONObject queryBody = new JSONObject();
        JSONObject boolBody = new JSONObject();
        JSONArray mustBodyArray = new JSONArray();


        //region 组装查询条件

        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getTrackGuid())) {
            JSONObject jsonObject = buildMatchPhraseQuery("trackGuid.keyword", queryAsyncFlowLogRequest.getTrackGuid());
            if (jsonObject != null) {
                mustBodyArray.add(jsonObject);
            }

        }

        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getBusinessSerialId())) {
            JSONObject jsonObject = buildMatchPhraseQuery("businessSerialId.keyword", queryAsyncFlowLogRequest.getBusinessSerialId());

            if (jsonObject != null) {
                mustBodyArray.add(jsonObject);
            }

        }

        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getAsyncFlowManagerName())) {
            JSONObject jsonObject = buildMatchPhraseQuery("flowManagerName.keyword", queryAsyncFlowLogRequest.getAsyncFlowManagerName());

            if (jsonObject != null) {
                mustBodyArray.add(jsonObject);
            }
        }
        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName())) {
            JSONObject jsonObject = buildMatchPhraseQuery("asyncFlowModule.keyword", queryAsyncFlowLogRequest.getAsyncFlowOperatorNodeName());

            if (jsonObject != null) {
                mustBodyArray.add(jsonObject);
            }
        }


        if (StringUtils.isNotBlank(queryAsyncFlowLogRequest.getEvn()) && !queryAsyncFlowLogRequest.getEvn().equals("all")) {
            JSONObject jsonObject = buildMatchPhraseQuery("environments.keyword", queryAsyncFlowLogRequest.getEvn());

            if (jsonObject != null) {
                mustBodyArray.add(jsonObject);
            }
        }
        JSONObject filterBody;
        filterBody = buildRangeQuery("asyncFlowDateTime", queryAsyncFlowLogRequest.getStartDate(), queryAsyncFlowLogRequest.getEndDate());

        if (mustBodyArray.size() > 0) {
            boolBody.put("must", mustBodyArray);
        }
        boolBody.put("filter", filterBody);

        //endregion

        //region 组装排序信息
        JSONObject sortBody = new JSONObject();
        JSONObject sortFieldBody = new JSONObject();
        sortFieldBody.put("order", "desc");
        sortBody.put("asyncFlowDateTime", sortFieldBody);
        searchJson.put("sort", sortBody);
        //endregion

        //region 组装分页信息
        searchJson.put("from", calculateOffset(queryAsyncFlowLogRequest));
        searchJson.put("size", queryAsyncFlowLogRequest.getPageSize());
        //endregion

        if (boolBody.containsKey("must") || boolBody.containsKey("filter")) {
            queryBody.put("bool", boolBody);
            searchJson.put("query", queryBody);
        } else {
            searchJson = null;
        }

        return searchJson;

    }

    private static JSONObject buildMatchPhraseQuery(String fieldName, String value) {
        if (StringUtils.isNotBlank(value)) {
            JSONObject match = new JSONObject();
            JSONObject mustKV = new JSONObject();
            mustKV.put(fieldName, value);
            match.put("match", mustKV);
            return match;
        }
        return null;
    }

    private static JSONObject buildRangeQuery(String fieldName, LocalDateTime startDate, LocalDateTime endDate) {
        JSONObject range = new JSONObject();
        JSONObject rangeKV = new JSONObject();

        JSONObject field = new JSONObject();
        if (startDate == null && endDate == null) {
            field.put("gt", 946656000000L);
        } else {
            if (startDate != null) {
                field.put("gt", LocalDateTime.of(startDate.toLocalDate(), LocalTime.MIN).toInstant(ZoneOffset.of("+0")).toEpochMilli());
            }
            if (endDate != null) {
                field.put("lt", endDate.toInstant(ZoneOffset.of("+0")).toEpochMilli());
            }

        }

        rangeKV.put(fieldName, field);
        range.put("range", rangeKV);
        return range;
    }


    /**
     * MongoDB监控实体转化为页面显示实体
     *
     * @param asyncFlowLogEntity MongoDB实体
     * @return 页面显示实体
     */
    private AsyncFlowLogVo getAsyncFlowNodeLogVo(AsyncFlowLogEntity asyncFlowLogEntity) {
        AsyncFlowLogVo asyncFlowLogVo = new AsyncFlowLogVo();
        List<AsyncFlowNodeMonitorVo> asyncFlowNodeMonitorVoListOrderBy = new ArrayList<>();
        asyncFlowLogVo.setTrackGuid(asyncFlowLogEntity.getTrackGuid());
        asyncFlowLogVo.setBusinessSerialId(asyncFlowLogEntity.getBusinessSerialId());
        asyncFlowLogVo.setLogTime(asyncFlowLogEntity.getLogTime());
        asyncFlowLogVo.setAsyncFlowName(asyncFlowLogEntity.getAsyncFlowModule());
        asyncFlowLogVo.setUseTime(asyncFlowLogEntity.getUseTime() + "ms");
        asyncFlowLogVo.setResult(asyncFlowLogEntity.isStatus() ? "成功" : "失败");
        asyncFlowLogVo.setLogInfo(asyncFlowLogEntity.getLogInfo());
        asyncFlowLogVo.setOperator(asyncFlowLogEntity.getOperator());
        asyncFlowLogVo.setEvn(asyncFlowLogEntity.getEnvironments());
        asyncFlowLogVo.setSourceDes(asyncFlowLogEntity.getSourceDes());
        asyncFlowLogVo.setFlowRequestJsonStr(asyncFlowLogEntity.getRequest());
        asyncFlowLogVo.setFlowResponseJsonStr(asyncFlowLogEntity.getResponse());
        Map<Integer, List<AsyncFlowNodeLogEntity>> stringListMap = asyncFlowLogEntity.getAsyncFlowNodeLogEntityList().stream().collect(Collectors.groupingBy(AsyncFlowNodeLogEntity::getExecuteEnumCode));
        for (Map.Entry<Integer, List<AsyncFlowNodeLogEntity>> listEntry : stringListMap.entrySet()) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            if (listEntry.getKey() == 2 || listEntry.getKey() == 7) {
                asyncFlowNodeMonitorVo.setNodeTypeOrderBy(2);
                asyncFlowNodeMonitorVo.setNodeTypeName("查询");
            } else {
                asyncFlowNodeMonitorVo.setNodeTypeOrderBy(listEntry.getKey());
                asyncFlowNodeMonitorVo.setNodeTypeName(ExecuteEnum.getEnumByCode(listEntry.getKey()).getMsg());
            }


            for (AsyncFlowNodeLogEntity asyncFlowNodeLogEntity : listEntry.getValue()) {
                AsyncFlowNodeLogVo asyncFlowNodeLogVo = new AsyncFlowNodeLogVo();
                asyncFlowNodeLogVo.setBusinessSerialId(asyncFlowNodeLogEntity.getBusinessSerialId());
                asyncFlowNodeLogVo.setNodeInfo(asyncFlowNodeLogEntity.getLogMethodName() + "(" + asyncFlowNodeLogEntity.getLogFileName() + ")" + "-" + (asyncFlowNodeLogEntity.isAsync() ? "异步" : "同步"));
                asyncFlowNodeLogVo.setLogFileName(asyncFlowNodeLogEntity.getLogFileName());
                asyncFlowNodeLogVo.setLogTime(DateUtil.htFormat_yyyyMMddHH_mm_ss(asyncFlowNodeLogEntity.getLogTime()));
                asyncFlowNodeLogVo.setResultMsg(asyncFlowNodeLogEntity.getLogMsg());
                asyncFlowNodeLogVo.setExecuteEnumCode(asyncFlowNodeLogEntity.getExecuteEnumCode());
                asyncFlowNodeLogVo.setUseTime(asyncFlowNodeLogEntity.getUseTimes());
                asyncFlowNodeLogVo.setCode(asyncFlowNodeLogEntity.getCode());
                if (asyncFlowNodeLogVo.getCode() != 200) {
                    asyncFlowNodeMonitorVo.setErrorCount(asyncFlowNodeMonitorVo.getErrorCount() + 1);
                    //存在失败的 且当前值是false赋值
                    if (asyncFlowNodeMonitorVo.isErrorHidden()) {
                        asyncFlowNodeMonitorVo.setErrorHidden(false);
                    }
                }

                for (AsyncFlowNodeDetailLogEntity asyncFlowNodeDetailLogEntity : asyncFlowNodeLogEntity.getAsyncFlowNodeDetailLogEntityList()) {
                    AsyncFlowNodeDetailLogVo asyncFlowNodeDetailLogVo = new AsyncFlowNodeDetailLogVo();
                    asyncFlowNodeDetailLogVo.setLogDetailName(asyncFlowNodeDetailLogEntity.getLogDetailName());
                    asyncFlowNodeDetailLogVo.setNodeLogDetailVisible(false);
                    asyncFlowNodeDetailLogVo.setNodeRequestJsonStr(asyncFlowNodeDetailLogEntity.getRequest());
                    asyncFlowNodeDetailLogVo.setNodeResponseJsonStr(asyncFlowNodeDetailLogEntity.getResponse());

                    asyncFlowNodeLogVo.getAsyncFlowNodeDetailLogVoList().add(asyncFlowNodeDetailLogVo);
                }
                asyncFlowNodeMonitorVo.getAsyncFlowNodeLogVoList().add(asyncFlowNodeLogVo);
            }
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);

        }

        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 1)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(1);
            asyncFlowNodeMonitorVo.setNodeTypeName("验证");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }

        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 2)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(2);
            asyncFlowNodeMonitorVo.setNodeTypeName("查询");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }
        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 3)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(3);
            asyncFlowNodeMonitorVo.setNodeTypeName("操作");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }
        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 4)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(4);
            asyncFlowNodeMonitorVo.setNodeTypeName("保存");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }
        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 5)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(5);
            asyncFlowNodeMonitorVo.setNodeTypeName("同步");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }
        if (asyncFlowNodeMonitorVoListOrderBy.stream().noneMatch(c -> c.getNodeTypeOrderBy() == 6)) {
            AsyncFlowNodeMonitorVo asyncFlowNodeMonitorVo = new AsyncFlowNodeMonitorVo();
            asyncFlowNodeMonitorVo.setNodeTypeOrderBy(6);
            asyncFlowNodeMonitorVo.setNodeTypeName("异步");
            asyncFlowNodeMonitorVo.setErrorCount(0);
            asyncFlowNodeMonitorVo.setErrorHidden(true);
            asyncFlowNodeMonitorVoListOrderBy.add(asyncFlowNodeMonitorVo);
        }
        asyncFlowLogVo.getAsyncFlowNodeMonitorVoList().addAll(asyncFlowNodeMonitorVoListOrderBy.stream().sorted(Comparator.comparing(AsyncFlowNodeMonitorVo::getNodeTypeOrderBy)).collect(Collectors.toList()));


        return asyncFlowLogVo;
    }


    /**
     * 获取所有的操作类型
     *
     * @return asyncFlowOperatorEnumNameVoList
     */
    @SneakyThrows
    public List<AsyncFlowOperatorEnumNameVo> getAsyncFlowNameList() {
        List<AsyncFlowOperatorEnumNameVo> asyncFlowOperatorEnumNameVoList = new ArrayList<>();
        List<String> asyncFlowNameList = new ArrayList<>();
        Yaml yaml = new Yaml();
        InputStream resourceAsStream = AsyncFlowVelocity.class.getClassLoader().getResourceAsStream("asyncflow.yml");
        Map obj = yaml.load(resourceAsStream);
        String basePackagePath = (String) obj.get("AsyncFlowExecutePackagePath");
        String loadManagerNodePackPath = basePackagePath + ".manager";
        Set<Class<?>> classes;
        classes = AsyncFlowReflectionUtils.getClasses(loadManagerNodePackPath);
        for (Class c : classes) {
            AsyncFlowOperatorEnumNameVo asyncFlowOperatorEnumNameVo = new AsyncFlowOperatorEnumNameVo();
            asyncFlowOperatorEnumNameVo.setFlowManagerName(c.getSimpleName());
            Annotation[] flowInfo = c.getAnnotations();
            for (Annotation annotation : flowInfo) {
                //获取方法名
                if (annotation instanceof FlowInfo) {
                    asyncFlowOperatorEnumNameVo.setFlowName(((FlowInfo) annotation).flowName());
                    break;
                }
            }
            ParameterizedType parameterizedType = (ParameterizedType) c.getGenericSuperclass();
            Type reqEnumClazz;
            if (parameterizedType.getActualTypeArguments().length == 4) {
                reqEnumClazz = parameterizedType.getActualTypeArguments()[3];
                Class<Enum> clazz = (Class<Enum>) reqEnumClazz;
                //获取所有枚举实例
                Enum[] enumConstants = clazz.getEnumConstants();
                //根据方法名获取方法
                Method getCode = clazz.getMethod("getCode");
                Method getMessage = clazz.getMethod("getMsg");
                for (Enum enum1 : enumConstants) {
                    //map = new HashMap<String, Object>();
                    //map.put("name",enum1.name());
                    ////执行枚举方法获得枚举实例对应的值
                    //map.put("code", getCode.invoke(enum1));
                    //map.put("msg", getMessage.invoke(enum1));
                    String asyncFlowModel = enum1.name() + "-" + getMessage.invoke(enum1);
                    if (asyncFlowModel.contains("默认") || asyncFlowModel.contains("defult")) {
                        continue;
                    }
                    asyncFlowOperatorEnumNameVo.getAsyncFlowNodeNameList().add(asyncFlowModel);
                }
                asyncFlowOperatorEnumNameVoList.add(asyncFlowOperatorEnumNameVo);
            }
        }


        return asyncFlowOperatorEnumNameVoList;
    }
}

